﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class ORDPROData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";

        public int ThreadCount;
        private string OrderNum;
        DataTable tbWGSummary;
        public ORDPROData()
            : base()
        {
            BuildTrisList();
            tbWGSummary = new DataTable("WGGSUM");
            tbWGSummary.Columns.Add("ARTICLE", typeof(string));
            tbWGSummary.Columns.Add("WBBDPART", typeof(string));
            tbWGSummary.Columns.Add("FACTORY", typeof(string));
            tbWGSummary.Columns.Add("WBEWMTR", typeof(double));
            tbWGSummary.Columns.Add("PCNUM", typeof(int));
        }
        public ORDPROData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        private void BuildTrisList()
        {
            ListTris = new ArrayList();
            TrisType tristype = null;
            TrisInclude = "01;10;11;13;21;27;32;";
            tristype = new TrisType("01", "+", "净长"); ListTris.Add(tristype);
            tristype = new TrisType("10", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("11", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("12", "-", "销售返回"); ListTris.Add(tristype);
            tristype = new TrisType("13", "+", "产品录入"); ListTris.Add(tristype);
            tristype = new TrisType("20", "-", "货物发出"); ListTris.Add(tristype);
            tristype = new TrisType("21", "+", "货物进入"); ListTris.Add(tristype);
            tristype = new TrisType("26", "-", "旧布卷减少"); ListTris.Add(tristype);
            tristype = new TrisType("27", "+", "新布卷增加"); ListTris.Add(tristype);
            tristype = new TrisType("31", "-", "发货"); ListTris.Add(tristype);
            tristype = new TrisType("32", "+", "退货"); ListTris.Add(tristype);
            tristype = new TrisType("36", "-", "生产消耗"); ListTris.Add(tristype);
            tristype = new TrisType("40", "-", "等级修改减少"); ListTris.Add(tristype);
            tristype = new TrisType("41", "-", "等级修改增加"); ListTris.Add(tristype);
            tristype = new TrisType("42", "+", "库存修正减少"); ListTris.Add(tristype);
            tristype = new TrisType("43", "+", "库存修正增加"); ListTris.Add(tristype);
            tristype = new TrisType("48", "-", "生产差异-"); ListTris.Add(tristype);
            tristype = new TrisType("49", "+", "生产差异+"); ListTris.Add(tristype);
            tristype = new TrisType("51", "+", "消耗差异"); ListTris.Add(tristype);
            tristype = new TrisType("61", "-", "留位"); ListTris.Add(tristype);
            tristype = new TrisType("62", "+", "取消留位"); ListTris.Add(tristype);
            tristype = new TrisType("64", "+", "留位剪布"); ListTris.Add(tristype);
            tristype = new TrisType("65", "+", "锁定"); ListTris.Add(tristype);
            tristype = new TrisType("66", "+", "解锁"); ListTris.Add(tristype);
            tristype = new TrisType("69", "+", "其他修正"); ListTris.Add(tristype);
            tristype = new TrisType("80", "+", "发票数量"); ListTris.Add(tristype);
            tristype = new TrisType("88", "-", "价值更改-"); ListTris.Add(tristype);
            tristype = new TrisType("89", "+", "价值更改"); ListTris.Add(tristype);
            tristype = new TrisType("IN", "+", "系统自动调节"); ListTris.Add(tristype);

        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE, ARTICLE, CUSTOMER;
            ARTICLE = dwABPOS["ARTICLE"].ToString().Trim();
            CUSTOMER = dwABPOS["CUSTOMER"].ToString().Trim();
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region

                or1.CommandText = "SELECT A.VKDELDATE,A.DIFFDATE,A.CUSTOMER,A.VKOPENDATE,A.CO,A.VKARTICLE,A.COQTY, "
                    + " substr(A.VKARTICLE,2,1) AS ARTICLEGROUP, "
                    + " A.ARTICLE,A.AMORDER,A.AMSTATUS,A.AMPROCESS,A.AMLASTDATE,A.AMFACTORY,A.AMARTICLE, "
                    + " A.AMSTARTDATE,A.AMACTSTARTDATE,A.AMENDDATE,A.AMACTENDDATE, "
                    + " A.VAORDER,A.VASTATUS,A.VAPROCESS,A.VALASTDATE,A.VAFACTORY,A.VAARTICLE, "
                    + " A.VASTARTDATE,A.VAACTSTARTDATE,A.VAENDDATE,A.VAACTENDDATE, "
                    + " A.VFORDER,A.VFSTATUS,A.VFPROCESS,A.VFLASTDATE,A.VFFACTORY,A.VFARTICLE, "
                    + " A.VFSTARTDATE,A.VFACTSTARTDATE,A.VFENDDATE,A.VFACTENDDATE, "
                    + " A.DIORDER,A.DISTATUS,A.DIPROCESS,A.DILASTDATE,A.DIFACTORY,A.DIARTICLE, "
                    + " A.DISTARTDATE,A.DIACTSTARTDATE,A.DIENDDATE,A.DIACTENDDATE, "
                    + " A.VWORDER,A.VWSTATUS,A.VWPROCESS,A.VWLASTDATE,A.VWFACTORY,A.VWARTICLE, "
                    + " A.VWSTARTDATE,A.VWACTSTARTDATE,A.VWENDDATE,A.VWACTENDDATE, "
                    + " A.DBORDER,A.DBSTATUS,A.DBPROCESS,A.DBLASTDATE,A.DBFACTORY,A.DBARTICLE, "
                    + " A.DBSTARTDATE,A.DBACTSTARTDATE,A.DBENDDATE,A.DBACTENDDATE, "
                    + " A.CBORDER,A.CBSTATUS,A.CBPROCESS,A.CBLASTDATE,A.CBFACTORY,A.CBARTICLE, "
                    + " A.CBSTARTDATE,A.CBACTSTARTDATE,A.CBENDDATE,A.CBACTENDDATE "
                    + " FROM ICC_ORDER_PROCESS A "
                    + " WHERE A.ARTICLE LIKE '%" + ARTICLE + "%' AND (CODE ='" + CUSTOMER + "' OR  '" + CUSTOMER + "' = '00000000') "
                    + " AND VKDELDATE>='" + WBSRTDATE + "' AND VKDELDATE<='" + WBENTDATE + "'"
                    ; 
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ORDPRO";
                    SetDefault(tableLocal);
                    byte[] b;
                   string value; 
                   string sTemp = ""; 
                   string Column = ""; 
                   foreach (DataRow dwTemp in tableLocal.Rows)
                   {
                       Column = "CUSTOMER";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "AMPROCESS";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "AMFACTORY";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "AMARTICLE";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion 
                       Column = "VAPROCESS";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "VAFACTORY";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "VFPROCESS";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "VFFACTORY";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "DIPROCESS";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "DIFACTORY";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "VWPROCESS";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "VWFACTORY";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "DBPROCESS";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "DBFACTORY";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "CBPROCESS";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "CBFACTORY";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "ARTICLEGROUP";
                       value = dwTemp[Column].ToString().Trim();
                       if(value == "1")
                       {
                           dwTemp[Column] = "市场组";
                       }
                       else if (value == "2" || value == "4")
                       {
                           dwTemp[Column] = "色织组";

                       }
                       else if (value == "3")
                       {
                           dwTemp[Column] = "高档组";

                       }
                       else if (value == "6")
                       {
                           dwTemp[Column] = "差异化组";

                       }
                       else
                       {
                           dwTemp[Column] = "新塘";

                       }

                   }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        
        public DataSet BuildBABST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}